package main

import "fmt"

func printSlice(s []int) {
	fmt.Printf("%v, len=%d, cap=%d\n",
		s, len(s), cap(s))
}

func main() {
	// createSlice()

	// createSlice2()

	// copySlice()

	deleteSlice()

	popFrontSlice()

	popTailSlice()
}

func popTailSlice() {
	s2 := []int{2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	tail := s2[len(s2)-1]
	s2 = s2[:len(s2)-1]
	fmt.Println(tail)
	printSlice(s2)
}

func popFrontSlice() {
	s2 := []int{2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	front := s2[0]
	s2 = s2[1:]
	fmt.Println(front)
	printSlice(s2)
}

func deleteSlice() {
	s2 := []int{2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	s2 = append(s2[:3], s2[4:]...)
	// :3 不包括3  4: 从4开始; 加三个点代表所有元素
	printSlice(s2)
}

func copySlice() {
	s1 := []int{2, 4, 6, 8}
	s2 := make([]int, 16)
	copy(s2, s1)
	// s1拷贝进s2
	printSlice(s2)
}

func createSlice2() {
	// 1.直接初始化
	s1 := []int{2, 4, 6, 8}
	fmt.Println(s1)

	// 2. 长度16
	s2 := make([]int, 16)

	// 3. 长度10 cap = 32(下面数组)
	s3 := make([]int, 10, 32)

	printSlice(s2)
	printSlice(s3)
}

// 定义一个slice，每个变量一定义就有zero value 即：nil
func createSlice() {
	var s []int
	for i := 0; i < 100; i++ {
		printSlice(s)
		s = append(s, i*2+1)
	}
	fmt.Println(s)
}
